package book;

public class SynchronizedTest2 {

	static class MyObject{
		
		private String username;
		private String password;
		
		synchronized public void set(String username, String password){
			
			this.username = username;
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.password = password;
			System.out.printf("设置,username=%s,password=%s\n", username, password);
		}
		
//		synchronized 
		public void get(){
			
			System.out.printf("获取,username=%s,password=%s\n", username, password);
		}
	}
	
	static class MyThread extends Thread{
		
		private MyObject object;
		
		public MyThread(MyObject object) {
			
			this.object = object;
		}
		
		public void run(){
			
			object.set("涂有", "杨瑞");
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		MyObject object = new MyObject();
		MyThread ta = new MyThread(object);
		
		ta.start();
		Thread.sleep(1000);
		object.get();
	}
}
